// pages/result/result.js
var config = require('../../config')
// const recordService = require('../../utils/recordService.js')
const recordService = require('../../services/recordService.js')
const sharedService = require('../../services/sharedService.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    questionNum: '',
    correctNum: '',
    errorNum: '',
    accuracy: '',
    resultState: true,
    serviceImg: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    that.setData({
      questionNum: options.qn,
      correctNum: options.cn,
      errorNum: options.en,
      accuracy: options.ac + '%',
      resultState: options.st
    }, function() {
      that.genRecord()
    })
  },

  genRecord: function() {
    const rightNum = this.data.correctNum
    const getGold = rightNum * config.basic.genGold

    const globalData = getApp().globalData
    const record = globalData.record

    record.gold = record.gold + getGold
    recordService.update(record, function (res) {
      globalData.record = record
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: '50% 50%',
      timingFunction: 'ease'
    })

    this.animation = animation;
    this.setData({
      serviceImg: animation.export()
    })

    var angle = 0;
    var direction = true;
    //连续动画需要添加定时器,所传参数每次+1就行
    setInterval(function () {
      if (direction) {
        angle = angle + 1;
        angle > 0 ? direction = false : direction = true;
      } else {
        angle = angle - 1;
        angle < 0 ? direction = true : direction = false;
      }
      this.animation.rotate(20 * (angle)).step();
      this.setData({
        serviceImg: this.animation.export()
      })
    }.bind(this), 600);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(options){
    return sharedService.onShareAppMessage(options, function() {
      recordService.get()
    });
  },
  continueBtn: function () {
    wx.reLaunch({
      url: '../index/index'
    })
  },
  examineBtn: function () {
    wx.redirectTo({
      url: '../score/score?qn=' + this.data.questionNum + '&cn=' + this.data.correctNum + '&en=' + this.data.errorNum + '&ac=' + this.data.accuracy + '&st=' + this.data.resultState
    })
  }
})
